package com.wthink.newMain.msg.inner

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.wthink.hzgj.R
import com.wthink.hzgj.data.domain.NoticeMsg
import com.wthink.hzgj.databinding.FragmentMsgInnerReaderBinding
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.hzgj.util.closeDefaultAnimator
import com.wthink.newMain.msg.inner.adapter.MsgAdapter
import com.wthink.newMain.msg.inner.viewmodel.ReadedViewmodel
import com.wthink.newMain.view.MsgDialog

class ReadedFragment : Fragment(){

    private val msgDialog: MsgDialog by lazy {
        MsgDialog(context!!)
    }

    private val viewModel by viewModels<ReadedViewmodel>()

    private lateinit var binding: FragmentMsgInnerReaderBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_msg_inner_reader,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.start()
        binding.readedRecycle.layoutManager = LinearLayoutManager(context,
            LinearLayoutManager.VERTICAL,false)

        binding.readedRecycle.adapter = MsgAdapter().apply {
            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    val noticeMsg = obj as NoticeMsg
                    showMsgDialog(noticeMsg)
                }
            }
        }
        closeDefaultAnimator(binding.readedRecycle)
    }

    private fun showMsgDialog(noticeMsg: NoticeMsg) {
         msgDialog.show()
         msgDialog.setOnclickListener(View.OnClickListener { v ->
             when (v?.id) {
                 R.id.close -> {
                     msgDialog.dismiss()
                 }
             }
         })
         msgDialog.setMsgText(noticeMsg.msg!!)
    }
}